#import "Core.h"
#import "PurposeUI.h"

@implementation PraySettingData

@synthesize rosaryNum = _rosaryNum;
@synthesize isLeftHanded = _isLeftHanded;
@synthesize enableLightMystery = _enableLightMystery;
@synthesize curDay = _curDay;
@synthesize startDate = _startDate;
@synthesize endDate = _endDate;
@synthesize language = _language;
@synthesize canVibrate = _canVibrate;
@synthesize biggerFont = _biggerFont;

- (id)initWithCoder:(NSCoder *)aDecoder
{
	if( (self = [super init]) != nil )
	{
		self.rosaryNum = [aDecoder decodeIntegerForKey:@"rosaryNum"];
		self.isLeftHanded = [aDecoder decodeBoolForKey:@"isLeftHanded"];
		self.enableLightMystery = [aDecoder decodeBoolForKey:@"enableLightMystery"];
		self.curDay = [aDecoder decodeIntegerForKey:@"curDay"];
		self.startDate = [aDecoder decodeObjectForKey:@"startDate"];
		self.endDate = [aDecoder decodeObjectForKey:@"endDate"];

		// 하위버전 호환
		if( [aDecoder containsValueForKey:@"language"] )
			self.language = [aDecoder decodeObjectForKey:@"language"];
		else
			self.language = @"kr";
		
		// Language : Español
		
		if( [aDecoder containsValueForKey:@"canVibrate"] )
			self.canVibrate = [aDecoder decodeBoolForKey:@"canVibrate"];
		else
			self.canVibrate = YES;

		if( [aDecoder containsValueForKey:@"biggerFont"] )
			self.biggerFont = [aDecoder decodeBoolForKey:@"biggerFont"];
		else
			self.biggerFont = NO;
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[aCoder encodeInteger:_rosaryNum forKey:@"rosaryNum"];
	[aCoder encodeBool:_isLeftHanded forKey:@"isLeftHanded"];
	[aCoder encodeBool:_enableLightMystery forKey:@"enableLightMystery"];
	[aCoder encodeInteger:_curDay forKey:@"curDay"];
	[aCoder encodeObject:_startDate forKey:@"startDate"];
	[aCoder encodeObject:_endDate forKey:@"endDate"];
	[aCoder encodeObject:_language forKey:@"language"];
	[aCoder encodeBool:_canVibrate forKey:@"canVibrate"];
	[aCoder encodeBool:_biggerFont forKey:@"biggerFont"];
}

+ (PraySettingData*)getSettingDataFromDefaultPath
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	PraySettingData* result;
	
	if( NO == [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, @"PraySetting.inf"]] )
	{
		result = [[PraySettingData alloc] init];
		result.rosaryNum = 1;
		result.isLeftHanded = NO;
		result.enableLightMystery = YES;
		result.curDay = 1;
		result.startDate = nil;
		result.endDate = nil;
		result.language = nil;
		result.canVibrate = YES;
		result.biggerFont = NO;
	}
	else 
	{
		result = (PraySettingData*)[[NSKeyedUnarchiver unarchiveObjectWithFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, @"PraySetting.inf"]] retain];
	}
	
	return result;
}

+ (void)saveSettingDataToDefaultPath:(PraySettingData *)data
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	[NSKeyedArchiver archiveRootObject:data toFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, @"PraySetting.inf"]];
}

@end

@implementation PraySettingNavigationController

- (id)initWithRootViewController:(PrayViewController *)rootViewController
{
	if ( (self = [super init]) != nil ) {
		_rootViewController = rootViewController;
		self.title = [_rootViewController getLocalizedStringForKey:@"환경설정"];
		self.viewControllers = [NSArray arrayWithObjects:[[PraySettingViewController alloc] initWithRootViewController:_rootViewController withStyle:UITableViewStyleGrouped], nil];
		self.navigationBar.tintColor = [UIColor colorWithRed:87.0f/255.0f green:77.0f/255.0f blue:78.0f/255.0f alpha:1.0f];
		
		CGRect rect = self.view.frame;
		rect.size.height = 382;
		self.view.frame = rect;
		
		self.delegate = self;
	}
	
	return self;
}

# pragma mark - UINavigationControllerDelegate Methods

- (void)navigationController:(UINavigationController *)navigationController 
	  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    static UIViewController *vcPointer = nil;
	
    // If the previous view controller is still around then let's send a viewWillDisappear message
    if (vcPointer != nil) {
        if ([vcPointer respondsToSelector:@selector(viewWillDisappear:)]) {
            [vcPointer viewWillDisappear:animated];
        }
    }
	
    // Keep track of a pointer to the current viewController
    vcPointer = viewController;
	
    [viewController viewWillAppear:animated];   
}

@end

@interface PraySettingViewController (private)

- (void)loadSetting;
- (void)saveSetting;
- (void)onSwitchBiggerFontChanged;
- (void)onSwitchLeftHandedChanged;
- (void)onSwitchLightMysteryChanged;
- (void)onSwitchVibrateChanged;

@end

@implementation PraySettingViewController (private)

- (void)loadSetting
{
	_settingData = [PraySettingData getSettingDataFromDefaultPath];
}

- (void)saveSetting
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	[NSKeyedArchiver archiveRootObject:_settingData toFile:[NSString stringWithFormat:@"%@/%@", documentsDirectory, @"PraySetting.inf"]];
}

- (void)onSwitchBiggerFontChanged
{
	_settingData.biggerFont = [_switchBiggerFont isOn];
	[self saveSetting];
	
	if( _delegate != nil )
	{
		[_delegate onSettingChanged:_settingData];
	}
}

- (void)onSwitchLeftHandedChanged
{
	_settingData.isLeftHanded = [_switchLeftHand isOn];
	[self saveSetting];
	
	if( _delegate != nil )
	{
		[_delegate onSettingChanged:_settingData];
	}
}

- (void)onSwitchLightMysteryChanged
{
	_settingData.enableLightMystery = [_switchLightMystery isOn];
	[self saveSetting];
	
	if( _delegate != nil )
	{
		[_delegate onSettingChanged:_settingData];
	}
}

- (void)onSwitchVibrateChanged
{
	_settingData.canVibrate = [_switchVibrate isOn];
	[self saveSetting];
	
	if( _delegate != nil )
	{
		[_delegate onSettingChanged:_settingData];
	}
}

@end

@implementation PraySettingViewController

@synthesize delegate = _delegate;

- (void)viewDidLoad
{
	[super viewDidLoad];
	
	self.title = [_rootViewController getLocalizedStringForKey:@"환경설정"];
	self.tableView.delegate = self;
	self.view.backgroundColor = [UIColor clearColor];
	_delegate = self.rootViewController;
	
	_backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
	_backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"setting_background.png"]];

	[self.navigationController.view addSubview:_backgroundView];
	[self.navigationController.view sendSubviewToBack:_backgroundView];
	
	[self loadSetting];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	switch (section) {
		case 0:
			return 1;
		case 1:
			return 1;
		case 2:
			return 3;
		case 3:
			return 1;
		case 4:
			return 2;
		default:
			return 0;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell*	cell;
	CGRect 				rect;
	
	// 지향기도 / 언어설정 / 테마변경 묵주변경 왼손잡이 / 빛의신비 / 진동 큰글씨
	switch (indexPath.section) {
		case 0 :
			cell = [tableView dequeueReusableCellWithIdentifier:@"PrayPurpose"];
			if( cell == nil )
			{
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PrayPurpose"] autorelease];
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			}
			cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"지향기도"];
			break;
		case 1 :
			cell = [tableView dequeueReusableCellWithIdentifier:@"Language"];
			if( cell == nil )
			{
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Language"] autorelease];
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			}
			cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"언어"];
			break;
		case 2 :
			if( indexPath.row == 0 )
			{
				cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeTheme"];
				if( cell == nil )
				{
					cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChangeTheme"] autorelease];
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				}
				cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"테마 변경"];
			}
			else if( indexPath.row == 1 )
			{
				cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeRosary"];
				if( cell == nil )
				{
					cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChangeRosary"] autorelease];
					cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				}
				cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"묵주 변경"];
			}
			else if( indexPath.row == 2 )
			{
				cell = [tableView dequeueReusableCellWithIdentifier:@"LeftHanded"];
				if( cell == nil )
				{
					cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LeftHanded"] autorelease];
					
					_switchLeftHand = [[UISwitch alloc] init];
					[_switchLeftHand setOn:NO];
					
					rect = _switchLeftHand.frame;
					rect.origin.x = SETTING_SWITCH_LEFT;
					rect.origin.y = (SETTING_ROWHEIGHT - rect.size.height) / 2;
					_switchLeftHand.frame = rect;
					
					[_switchLeftHand addTarget:self action:@selector(onSwitchLeftHandedChanged) forControlEvents:UIControlEventValueChanged];
					
					if( _settingData.isLeftHanded )
						[_switchLeftHand setOn:YES];
					
					[cell.contentView addSubview:_switchLeftHand];
				}
				cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"왼손잡이"];
				[cell.contentView bringSubviewToFront:_switchLeftHand];
			}
			break;
		case 3 :
			cell = [tableView dequeueReusableCellWithIdentifier:@"LightMystery"];
			if( cell == nil )
			{
				cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LightMystery"] autorelease];
				
				_switchLightMystery = [[UISwitch alloc] init];
				[_switchLightMystery setOn:NO];
				
				rect = _switchLightMystery.frame;
				rect.origin.x = SETTING_SWITCH_LEFT;
				rect.origin.y = (SETTING_ROWHEIGHT - rect.size.height) / 2;
				_switchLightMystery.frame = rect;
				
				[_switchLightMystery addTarget:self action:@selector(onSwitchLightMysteryChanged) forControlEvents:UIControlEventValueChanged];
				
				if( _settingData.enableLightMystery )
					[_switchLightMystery setOn:YES];
				
				[cell.contentView addSubview:_switchLightMystery];
			}
			cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"빛의 신비 활성화"];
			[cell.contentView bringSubviewToFront:_switchLightMystery];
			break;
		case 4:// 진동 큰글씨
			if( indexPath.row == 0 )
			{
				cell = [tableView dequeueReusableCellWithIdentifier:@"Vibrate"];
				if( cell == nil )
				{
					cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Vibrate"] autorelease];
					
					_switchVibrate = [[UISwitch alloc] init];
					[_switchVibrate setOn:YES];
					
					rect = _switchVibrate.frame;
					rect.origin.x = SETTING_SWITCH_LEFT;
					rect.origin.y = (SETTING_ROWHEIGHT - rect.size.height) / 2;
					_switchVibrate.frame = rect;
					
					[_switchVibrate addTarget:self action:@selector(onSwitchVibrateChanged) forControlEvents:UIControlEventValueChanged];
					
					if( _settingData.canVibrate )
						[_switchVibrate setOn:YES];
					
					[cell.contentView addSubview:_switchVibrate];
				}
				cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"진동"];
				[cell.contentView bringSubviewToFront:_switchVibrate];
			}
			else if( indexPath.row == 1 )
			{
				cell = [tableView dequeueReusableCellWithIdentifier:@"BiggerFont"];
				if( cell == nil )
				{
					cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BiggerFont"] autorelease];
					
					_switchBiggerFont = [[UISwitch alloc] init];
					[_switchBiggerFont setOn:YES];
					
					rect = _switchBiggerFont.frame;
					rect.origin.x = SETTING_SWITCH_LEFT;
					rect.origin.y = (SETTING_ROWHEIGHT - rect.size.height) / 2;
					_switchBiggerFont.frame = rect;
					
					[_switchBiggerFont addTarget:self action:@selector(onSwitchBiggerFontChanged) forControlEvents:UIControlEventValueChanged];
					
					if( _settingData.biggerFont )
						[_switchBiggerFont setOn:YES];
					
					[cell.contentView addSubview:_switchBiggerFont];
				}
				cell.textLabel.text = [_rootViewController getLocalizedStringForKey:@"큰 글씨"];
				[cell.contentView bringSubviewToFront:_switchBiggerFont];
			}
			break;
		default:
			break;
	}
	
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return SETTING_ROWHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	PurposeHistoryViewController*	purposeHistoryView;
	[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	// 지향기도 / 언어설정 / 테마변경 묵주변경 왼손잡이 / 빛의신비 / 진동 큰글씨
	switch (indexPath.section) {
		case 0:
			purposeHistoryView = [[PurposeHistoryViewController alloc] initWithRootViewController:_rootViewController withStyle:UITableViewStylePlain andFileName:@"nineDayPurposeHistory.inf"];
			[self.navigationController pushViewController:purposeHistoryView animated:YES];
			[purposeHistoryView release];
			break;
		case 1:
			if( indexPath.row == 0 )
			{
				SelectLanguageViewController*	selectLanguage = [[SelectLanguageViewController alloc] initWithRootViewController:_rootViewController language:_settingData.language];
				selectLanguage.delegate = self;
																  
				[self.navigationController pushViewController:selectLanguage animated:YES];	
																  
				[selectLanguage release];
			}
			break;
		case 2:
			if( indexPath.row == 0 )
			{
			
			}
			else if( indexPath.row == 1 )
			{
				SelectRosaryViewController*	selectRosary = [[SelectRosaryViewController alloc] initWithRootViewController:_rootViewController 
																												   rosary:_settingData.rosaryNum];
				selectRosary.delegate = self;
				
				[self.navigationController pushViewController:selectRosary animated:YES];	
				
				[selectRosary release];	
			}
			break;
		default:
			break;
	}
}

- (void)onRosarySelected:(NSInteger)rosaryNum
{
	_settingData.rosaryNum = rosaryNum;
	
	[self saveSetting];
	
	if( _delegate != nil )
	{
		[_delegate onSettingChanged:_settingData];
	}
}

- (void)onLanguageSelected:(NSString*)language
{
	_settingData.language = language;
	
	[self saveSetting];
	
	if( _delegate != nil )
	{
		[_delegate onSettingChanged:_settingData];
	}
	
	self.title = [_rootViewController getLocalizedStringForKey:@"환경설정"];
	[self.tableView reloadData];
}

-(void)viewDidUnload
{
	[_backgroundView removeFromSuperview];
	[_backgroundView release];
}

@end

@interface SelectRosaryViewController (private)

- (void)initSubViews;
- (void)onSave;
- (void)onRosary1;
- (void)onRosary2;
- (void)onRosary3;

@end

@implementation SelectRosaryViewController (private)

- (void)initSubViews
{
	_bigRosaryView = [[UIImageView alloc] initWithFrame:CGRectMake(44.5, 35, 125, 291)];
	_bigRosaryView.image = [UIImage imageNamed:SETTING_IMG_ROSARY1_BIG];
	_bigRosaryView.backgroundColor = [UIColor clearColor];
	[self.view addSubview:_bigRosaryView];
	
	_rosary1 = [[ImageButton alloc] initWithFrame:CGRectMake(209, 13, 96, 101) 
				   andShowThisImageForNormalState:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]
					  thenShowThisWhenHighlighted:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]
											  let:self 
									handleEventAt:@selector(onRosary1)];
	_rosary2 = [[ImageButton alloc] initWithFrame:CGRectMake(209, 120, 96, 101) 
				   andShowThisImageForNormalState:[UIImage imageNamed:SETTING_IMG_ROSARY2_OFF]
					  thenShowThisWhenHighlighted:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]
											  let:self 
									handleEventAt:@selector(onRosary2)];
	_rosary3 = [[ImageButton alloc] initWithFrame:CGRectMake(209, 227, 96, 101) 
				   andShowThisImageForNormalState:[UIImage imageNamed:SETTING_IMG_ROSARY3_OFF]
					  thenShowThisWhenHighlighted:[UIImage imageNamed:SETTING_IMG_ROSARY3_ON]
											  let:self 
									handleEventAt:@selector(onRosary3)];
	[self.view addSubview:_rosary1];
	[self.view addSubview:_rosary2];
	[self.view addSubview:_rosary3];
	
	switch (_selectedRosaryNum) {
		case 1:
			[self onRosary1];
			break;
		case 2:
			[self onRosary2];
			break;
		case 3:
			[self onRosary3];
			break;
		default:
			[self onRosary1];
			break;
	}
}

- (void)onSave
{
	if( self.delegate != nil )
		[self.delegate onRosarySelected:_selectedRosaryNum];
	
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)onRosary1
{
	[_rosary1 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]];
	[_rosary1 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]];
	[_rosary2 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_OFF]];
	[_rosary2 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_ON]];
	[_rosary3 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_OFF]];
	[_rosary3 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_ON]];
	
	[_bigRosaryView setImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_BIG]];
	
	_selectedRosaryNum = 1;
}

- (void)onRosary2
{
	[_rosary1 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_OFF]];
	[_rosary1 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]];
	[_rosary2 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_ON]];
	[_rosary2 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_ON]];
	[_rosary3 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_OFF]];
	[_rosary3 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_ON]];
	
	[_bigRosaryView setImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_BIG]];	
	
	_selectedRosaryNum = 2;
}

- (void)onRosary3
{
	[_rosary1 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_OFF]];
	[_rosary1 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY1_ON]];
	[_rosary2 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_OFF]];
	[_rosary2 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY2_ON]];
	[_rosary3 setNormalImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_ON]];
	[_rosary3 setHighlightedImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_ON]];
	
	[_bigRosaryView setImage:[UIImage imageNamed:SETTING_IMG_ROSARY3_BIG]];	
	
	_selectedRosaryNum = 3;
}

@end

@implementation SelectRosaryViewController

@synthesize delegate = _delegate;

- (id)initWithRootViewController:(PrayViewController *)rootViewController rosary:(NSInteger)rosaryNum
{
	self = [super initWithRootViewController:rootViewController];
	
	if( self != nil )
	{
		_selectedRosaryNum = rosaryNum;
	}
	
	return self;
}

- (void)viewDidLoad
{
	self.title = [_rootViewController getLocalizedStringForKey:@"묵주선택"];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(onSave)];
	self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"setting_background.png"]];
	
	[self initSubViews];
}

// hide context menu for UITextField.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
	UIMenuController*	menuController = [UIMenuController sharedMenuController];
	
	if (nil != menuController)
	{
		[UIMenuController sharedMenuController].menuVisible	= NO;
	}
	
	return NO;
}

@end

@interface SelectLanguageViewController (private)

- (void)onSave;

@end

@implementation SelectLanguageViewController (private)

- (void)onSave
{
	if( self.delegate != nil )
		[self.delegate onLanguageSelected:[LanguageInformation getLanguageCodeFromLanguageName:_selectedLanguage]];
	
	[self.navigationController popViewControllerAnimated:YES];
}

@end

@implementation SelectLanguageViewController

@synthesize delegate = _delegate;																  

- (id)initWithRootViewController:(PrayViewController *)rootViewController language:(NSString *)language
{
	self = [super initWithRootViewController:rootViewController withStyle:UITableViewStyleGrouped];
	
	if( self != nil )
	{
		_selectedLanguage = [LanguageInformation getLanguageNameFromLanguageCode:language];
		_languageList = [[NSArray alloc] initWithObjects:@"한국어", @"English", @"Español", nil];
	}
	
	return self;
}

- (void)viewDidLoad
{
	self.title = [_rootViewController getLocalizedStringForKey:@"언어설정"];
	self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(onSave)];
	self.tableView.delegate = self;
	self.tableView.backgroundColor = [UIColor clearColor];

	_backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
	_backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"setting_background.png"]];
	
	[self.navigationController.view addSubview:_backgroundView];
	[self.navigationController.view sendSubviewToBack:_backgroundView];
}

- (void)viewDidUnload
{
	[_backgroundView removeFromSuperview];
	[_backgroundView release];
	
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
{
	return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
	UITableViewCell *cell;
	
	cell = [tableView dequeueReusableCellWithIdentifier:@"default"];

	if( cell == nil )
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"default"] autorelease];
	
	cell.accessoryType = UITableViewCellAccessoryNone;
	cell.textLabel.text = [_languageList objectAtIndex:indexPath.row];
//	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	if( [cell.textLabel.text isEqualToString:_selectedLanguage] )
	{
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
		_selectedCell = indexPath.row;
	}
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	UITableViewCell* cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedCell inSection:0]];
	cell.accessoryType = UITableViewCellAccessoryNone;
	
	_selectedLanguage = [_languageList objectAtIndex:indexPath.row];
	_selectedCell = indexPath.row;
	cell = [tableView cellForRowAtIndexPath:indexPath];
	cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

// hide context menu for UITextField. What is this??
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
	UIMenuController*	menuController = [UIMenuController sharedMenuController];
	
	if (nil != menuController)
	{
		[UIMenuController sharedMenuController].menuVisible	= NO;
	}
	
	return NO;
}

@end


